package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.adapter.ProjectGongsiNameAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.ProjectGongsiInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 项目经验公司名称
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class ProjectGongsiNameActivity extends RequestActivity {

	LinearLayout llayout;

	TextView addcompany, savedata, del;
	List<ProjectGongsiInfoBean> messageInfo;

	ProjectGongsiNameAdapter adapter;

	ListView listview;

	private boolean isRefresh = false;

	public static List<ProjectGongsiInfoBean> Xuanzhongmessage;

	boolean isbianji = true;
	ImageView back;
	boolean xiazaiwanbi = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		messageInfo = new ArrayList<ProjectGongsiInfoBean>();
		Xuanzhongmessage = new ArrayList<ProjectGongsiInfoBean>();
		initView();
		launchRequest(WooRequestFactory.getProjectGongsi());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		del = (TextView) this.findViewById(R.id.del);
		del.setOnClickListener(this);
		listview = (ListView) this.findViewById(R.id.listview);
		// adapter = new ProjectGongsiNameAdapter(this, list, Xuanzhongmessage);
		// listview.setAdapter(adapter);

		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		savedata = (TextView) this.findViewById(R.id.savedata);
		savedata.setOnClickListener(this);
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.RESPONSE_PROJECTGONGSI_DATA)) {
			messageInfo = new ArrayList<ProjectGongsiInfoBean>();
			Xuanzhongmessage = new ArrayList<ProjectGongsiInfoBean>();
			List<ProjectGongsiInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_PROJECTGONGSI_DATA);
			if (retList != null)
				messageInfo = retList;
			System.out.println("messageInfo长度：" + messageInfo.size());
			ProjectGongsiInfoBean pfib = new ProjectGongsiInfoBean();
			pfib.setComName("");
			pfib.setProjectCount(0);
			pfib.isselected = 0;
			messageInfo.add(pfib);
			System.out.println("长度：" + messageInfo.size());
			adapter = new ProjectGongsiNameAdapter(this, messageInfo,
					Xuanzhongmessage);
			listview.setAdapter(adapter);
			xiazaiwanbi = true;
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 1) {
				launchRequest(WooRequestFactory.getProjectGongsi());
			} else {
				Toast.makeText(this, "修改失败!", Toast.LENGTH_LONG).show();
			}

		} else if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_DELGONGSI)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_DELGONGSI) == 1) {
				System.out.println("删除成功！");
				launchRequest(WooRequestFactory.getProjectGongsi());
				isbianji = true;
				savedata.setText("编辑");
				if (adapter != null) {
					adapter.hiddenButton();
					del.setVisibility(View.GONE);
				}
			}
		}
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.projectgongsiname_layout;

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (xiazaiwanbi == false)
				finish();
			else
				backActivity();
			return true;

		}

		return super.onKeyDown(keyCode, event);

	}

	private void backActivity() {
		String name = "";
		System.out.println("messageInfo.size():" + messageInfo.size());
		Intent intent = new Intent();
		if (messageInfo.size() > 2) {

			if (!messageInfo.get(0).getComID().equals("")) {
				name = selectData_info("t_dictionary", "id = "
						+ messageInfo.get(0).getComID())
						+ "等" + (messageInfo.size() - 1) + "个";
			} else {
				name = messageInfo.get(0).getComName() + "等"
						+ (messageInfo.size() - 1) + "个";
			}

			intent.putExtra("name", name);

		} else if (messageInfo.size() == 2) {
			if (!messageInfo.get(0).getComID().equals("")) {
				name = selectData_info("t_dictionary", "id = "
						+ messageInfo.get(0).getComID());
			} else {
				name = messageInfo.get(0).getComName();
			}
			intent.putExtra("name", name);
		} else {
			intent.putExtra("name", "");
		}
		setResult(105, intent);
		finish();
	}

	private String selectData_info(String tabname, String selection) {
		String name = "";
		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				"orders");
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor.getColumnIndex("name_cn"));
		}
		cursor.close();
		db.close();
		dh.close();
		return name;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			if (xiazaiwanbi == false)
				finish();
			else
				backActivity();
			break;
		case R.id.savedata:// 保存
			if (isbianji) {
				isbianji = false;
				savedata.setText("保存");
				if (adapter != null) {
					adapter.showButton();
					del.setVisibility(View.VISIBLE);
				}
			} else {
				isbianji = true;
				savedata.setText("编辑");
				if (adapter != null) {
					adapter.hiddenButton();
					del.setVisibility(View.GONE);
				}
			}

			break;
		case R.id.del:
			if (Xuanzhongmessage.size() > 0) {
				String deleteUserExpCom = Xuanzhongmessage.get(0).getId();
				for (int i = 1; i < Xuanzhongmessage.size(); i++) {
					deleteUserExpCom += "," + Xuanzhongmessage.get(i).getId();
				}
				launchRequest(WooRequestFactory.delGongsi(deleteUserExpCom));
			}

			break;

		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 110:
			int position = data.getIntExtra("position", -1);
			int size = data.getIntExtra("size", 0);
			if (position != -1) {
				messageInfo.get(position).setProjectCount(size - 1);
				adapter.notifyDataSetChanged();
			}
			break;
		case 10010:
			System.out.println("data.getStringExtra():"
					+ data.getStringExtra("name"));
			String name = data.getStringExtra("name");
			String nameid = data.getStringExtra("nameid");

			if (!nameid.equals("")) {
				launchRequest(WooRequestFactory.addGongsi("comID", nameid));
			} else {
				launchRequest(WooRequestFactory.addGongsi("comName", name));
			}
			isbianji = true;
			savedata.setText("编辑");
			if (adapter != null) {
				adapter.hiddenButton();
				del.setVisibility(View.GONE);
			}

			break;
		default:
			break;
		}
	}
}
